home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / smaltalk / gnu_st.lha / gnu_st / smalltalk-1.1.1 / Time.st < prev    next >
Text File  |  1991-09-12  |  3KB  |  125 lines

  1. "======================================================================
  2. |
  3. |   Time Method Definitions
  4. |
  5.  ======================================================================"
  6.  
  7.  
  8. "======================================================================
  9. |
  10. | Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  11. | Written by Steve Byrne.
  12. |
  13. | This file is part of GNU Smalltalk.
  14. |
  15. | GNU Smalltalk is free software; you can redistribute it and/or modify it
  16. | under the terms of the GNU General Public License as published by the Free
  17. | Software Foundation; either version 1, or (at your option) any later version.
  18. | GNU Smalltalk is distributed in the hope that it will be useful, but WITHOUT
  19. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  20. | FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
  21. | details.
  22. | You should have received a copy of the GNU General Public License along with
  23. | GNU Smalltalk; see the file COPYING.  If not, write to the Free Software
  24. | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  
  25. |
  26.  ======================================================================"
  27.  
  28.  
  29. "
  30. |     Change Log
  31. | ============================================================================
  32. | Author       Date       Change 
  33. | sbyrne     24 Apr 90      Fixed printOn: to print the instance time, instead of
  34. |              now, and added printOn: for Time class to print now.
  35. |
  36. | sbyrne     19 Sep 89      Changed to use real method categories.
  37. |
  38. | sbyrne     12 Aug 89      Implemented many methods.  The book is exceptionally
  39. |              vague here, so please feel free to change the
  40. |              behavior to something which is more correct.
  41. |
  42. | sbyrne     25 Apr 89      created.
  43. |
  44. "
  45.  
  46. Magnitude subclass: #Time
  47.       instanceVariableNames: 'seconds'
  48.       classVariableNames: ''
  49.       poolDictionaries: ''
  50.       category: nil.
  51.  
  52. Time comment: 
  53. 'My instances represent times of the day.  I provide methods for instance 
  54. creation, methods that access components (hours, minutes, and seconds) of a 
  55. time value, and a block execution timing facility.' !
  56.  
  57. !Time class methodsFor: 'basic'!
  58.  
  59. now
  60.     ^self new setSeconds: Time secondClock
  61. !
  62.  
  63. fromSeconds: secondCount
  64.     ^self new setSeconds: (Time secondClock \\ (24*60*60)) + secondCount
  65. !
  66.  
  67. millisecondClockValue
  68.     ^self millisecondClock
  69. !
  70.  
  71. millisecondsToRun: timedBlock
  72.     | startTime|
  73.     startTime _ self millisecondClock.
  74.     timedBlock value.
  75.     ^self millisecondClock - startTime
  76. !!
  77.  
  78.  
  79.  
  80. !Time methodsFor: 'accessing'!
  81.  
  82. hours
  83.     ^(seconds // (60*60)) \\ 24
  84. !
  85.  
  86. minutes
  87.     ^(seconds // 60) \\ 60
  88. !
  89.  
  90. seconds
  91.     ^seconds \\ 60
  92. !!
  93.  
  94.  
  95.  
  96. !Time methodsFor: 'arithmetic'!
  97.  
  98. addTime: timeAmount
  99.     ^Time new setSeconds: seconds + timeAmount
  100. !
  101.  
  102. subtractTime: timeAmount
  103.     ^Time new setSeconds: seconds - timeAmount
  104. !
  105.  
  106. printOn: aStream
  107.     self hours printOn: aStream.
  108.     aStream nextPut: $:.
  109.     self minutes < 10 ifTrue: [ aStream nextPut: $0 ].
  110.     self minutes printOn: aStream.
  111.     aStream nextPut: $:.
  112.     self seconds < 10 ifTrue: [ aStream nextPut: $0 ].
  113.     self seconds printOn: aStream
  114. !!
  115.  
  116.  
  117.  
  118. !Time methodsFor: 'private'!
  119.  
  120. setSeconds: secs
  121.     seconds _ secs
  122. !!
  123.